package com.gengdan.service.impl;

import com.gengdan.bean.CollecProcMode;
import com.gengdan.bean.SampleCollProcMsg;
import com.gengdan.bean.SampleCollec;
import com.gengdan.bean.SampleCollecProcInsert;
import com.gengdan.dao.SampleCollecDao;
import com.gengdan.service.SampleCollecService;
import com.gengdan.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ShiJiaWei
 * @version 1.0
 * @date 2020/4/20 21:58
 */
@Service
public class SampleCollecServiceImpl implements SampleCollecService {
    @Autowired
    private SampleCollecDao sampleCollecDao;

    public HashMap<String, Object> selectPersonalRegister(int userId) {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();

        List<SampleCollec> personalRegisterList = sampleCollecDao.selectPersonalRegister(userId);
        if(personalRegisterList.isEmpty()){
            hashMap.put("status","0");
            hashMap.put("msg","查询失败");
            hashMap.put("showPersonalRegister",personalRegisterList);
            return hashMap;
        }

        hashMap.put("status","1");
        hashMap.put("msg","查询成功");
        hashMap.put("showPersonalRegister",personalRegisterList);
        return hashMap;
    }

    @Override
    public HashMap<String, Object> selectSampleCollec(int userId) {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        List<SampleCollec> SampleCollecList = sampleCollecDao.selectSampleCollec(userId);

        if(SampleCollecList.isEmpty()){
            hashMap.put("status","0");
            hashMap.put("msg","查询失败");
            hashMap.put("showSampleCollec",SampleCollecList);
            return hashMap;
        }

        hashMap.put("status","1");
        hashMap.put("msg","查询成功");
        hashMap.put("showSampleCollec",SampleCollecList);
        return hashMap;
    }

    /***
    * @Description 采集处理模板表格显示
    * @param
    * @Return java.util.HashMap<java.lang.String,java.lang.Object>
    * @Author Sun Mingshan
    * @Date 2020/5/9 14:43
    **/
    @Override
    public HashMap<String, Object> selectSampleCollecProcMsg(Page page) {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        com.github.pagehelper.Page<Object> pageHelper= PageHelper.startPage(page.getStart(),page.getCount());
        //        排序
        //        PageHelper.orderBy("id ASC");
        List<SampleCollProcMsg> SampleCollecProcMsg = sampleCollecDao.selectCollecMsg();
        long total = pageHelper.getTotal();
        page.caculateLast(total);
        //测试
//        System.out.println(total);
//        System.out.println(page.getCount());
//        System.out.println(page.getStart());

        if(SampleCollecProcMsg.isEmpty()){
            hashMap.put("status","0");
            hashMap.put("msg","查询失败");
            hashMap.put("showSampleCollecProcMsg",SampleCollecProcMsg);
            return hashMap;
        }

        hashMap.put("status","1");
        hashMap.put("msg","查询成功");
        hashMap.put("showSampleCollecProcMsg",SampleCollecProcMsg);
        return hashMap;
    }

    /***
    * @Description 采集处理模板高级检索
    * @param keyWord 关键字
    * @param page 分页信息（需传首页）
    * @Return java.util.HashMap<java.lang.String,java.lang.Object>
    * @Author Sun Mingshan
    * @Date 2020/5/9 18:45
    **/
    @Override
    public HashMap<String, Object> selectCollecMsgByKey(String keyWord, Page page) {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        com.github.pagehelper.Page<Object> pageHelper= PageHelper.startPage(page.getStart(),page.getCount());
        //        排序
        //        PageHelper.orderBy("id ASC");
        String indexKeyWord = "%"+keyWord+"%";
        List<SampleCollProcMsg> SampleCollecProcMsg = sampleCollecDao.selectCollecMsgByKey(indexKeyWord);
        long total = pageHelper.getTotal();
        page.caculateLast(total);

        if(SampleCollecProcMsg.isEmpty()){
            hashMap.put("status","0");
            hashMap.put("msg","查询失败");
            hashMap.put("showSampleCollecProcMsg",SampleCollecProcMsg);
            return hashMap;
        }

        hashMap.put("status","1");
        hashMap.put("msg","查询成功");
        hashMap.put("showSampleCollecProcMsg",SampleCollecProcMsg);
        return hashMap;
    }

    @Override
    public HashMap<String, Object> getCollecProcMode() {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        List<CollecProcMode> CollecProjectNameMsg = sampleCollecDao.getCollecProjectNameMode();
        List<CollecProcMode> ProcModeMsg = sampleCollecDao.getProcNameMode();

        if(CollecProjectNameMsg.isEmpty()){
            hashMap.put("status","0");
            hashMap.put("msg","查询失败");
            hashMap.put("showCollecProjectNameMsg",CollecProjectNameMsg);
            hashMap.put("showCollecProcModeMsg",ProcModeMsg);
            return hashMap;
        }

        hashMap.put("status","1");
        hashMap.put("msg","查询成功");
        hashMap.put("showCollecProjectNameMsg",CollecProjectNameMsg);
        hashMap.put("showCollecProcModeMsg",ProcModeMsg);
        return hashMap;
    }

    /***
    * @Description 采集处理插入数据【入库员】
    * @param map
    * @Return java.util.HashMap<java.lang.String,java.lang.Object>
    * @Author Sun Mingshan
    * @Date 2020/5/10 14:32
    **/
    @Override
    @Transactional(propagation= Propagation.REQUIRED,rollbackForClassName="Exception")
    public HashMap<String, Object> insertSampleProc(Map map) {
        LinkedHashMap sampleCollecList = (LinkedHashMap) map.get("insertCollecSample");
        //初始化处理结果（循环中如果一直成功则为1，有中断为0）
        int insertProcResult = 0;
        //获取采集样品数据，赋值
        Object projectName = (Object) sampleCollecList.get("projectName");
        Object collecTime = (Object) sampleCollecList.get("collecTime");
        Object collecName = (Object) sampleCollecList.get("collecName");
        Object collecNum = (Object) sampleCollecList.get("collecNum");
        Object collecQuantity = (Object) sampleCollecList.get("collecQuantity");
        Object collecCategory = (Object) sampleCollecList.get("collecCategory");
        Object collecUserId = (Object) sampleCollecList.get("userId");
        Object collecHospital = (Object) sampleCollecList.get("hospital");
        Object collecDepartment = (Object) sampleCollecList.get("department");
        //实例化一个采集处理对象
        SampleCollecProcInsert sampleCollecInsert = new SampleCollecProcInsert();
        //对象赋值
        sampleCollecInsert.setProjectName(projectName.toString());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            sampleCollecInsert.setCollecTime(sdf.parse(collecTime.toString()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        sampleCollecInsert.setCollecName(collecName.toString());
        sampleCollecInsert.setCollecNum(Integer.parseInt(collecNum.toString()));
        sampleCollecInsert.setCollecQuantity(Double.parseDouble(collecQuantity.toString()));
        sampleCollecInsert.setCollecCategory(collecCategory.toString());
        sampleCollecInsert.setUserId(Integer.parseInt(collecUserId.toString()));
        sampleCollecInsert.setDepartment(collecDepartment.toString());
        sampleCollecInsert.setHospital(collecHospital.toString());
//        打印测试
        System.out.println(sampleCollecInsert.toString());
        //循环次数在大小上-1(减去采集数)
        for(int i = 1;i<map.size();i++) {
            String value = "insertProcSample";
            //拼接字符串
            String number = Integer.toString(i);
            String result = value.concat(number);
            System.out.println(result);
            //拼接字符串  end
            LinkedHashMap sampleProcList = (LinkedHashMap) map.get(result);
            System.out.println("!!!!"+sampleProcList.toString());
            //获取处理样品数据，赋值
            Object collecId  =  sampleProcList.get("collecId");
            Object procTime  =  sampleProcList.get("procTime");
            Object countryCode =  sampleProcList.get("countryCode");
            Object procName =  sampleProcList.get("procName");
            Object procQuantity = sampleProcList.get("procQuantity");
            Object unit = sampleProcList.get("unit");
            Object proNum = sampleProcList.get("proNum");
            System.out.println(collecId);
            System.out.println(procTime);
            System.out.println(countryCode);
            System.out.println(procName);
            System.out.println(procQuantity);
            System.out.println(unit);
            System.out.println(proNum);

            SampleCollecProcInsert sampleProcInsert = new SampleCollecProcInsert();
            sampleProcInsert.setCollecId(Integer.parseInt(collecId.toString()));
            //日期转换器
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                sampleProcInsert.setProcTime((sdf2).parse(procTime.toString()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            sampleProcInsert.setCountryCode(countryCode.toString());
            sampleProcInsert.setProcName(procName.toString());
            sampleProcInsert.setProcQuantity(Integer.parseInt(procQuantity.toString()));
            sampleProcInsert.setUnit(unit.toString());
            sampleProcInsert.setProcNum(Integer.parseInt(proNum.toString()));

            System.out.println(sampleProcInsert.getCollecId());
            System.out.println(sampleProcInsert.getCountryCode());
            System.out.println(sampleProcInsert.getProcName());
            System.out.println(sampleProcInsert.getProcQuantity());
            System.out.println(sampleProcInsert.getUnit());
            System.out.println(sampleProcInsert.getProcNum());
//        打印测试
            System.out.println(sampleProcInsert.toString());
            insertProcResult = sampleCollecDao.insertSampleProc(sampleProcInsert);

        }
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        int insertCollecResult = sampleCollecDao.insertSampleCollect(sampleCollecInsert);
        //最后只要有中断失败
        if(insertCollecResult==0 || insertProcResult==0){
            //roll back
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            hashMap.put("status","0");
            hashMap.put("msg","插入失败");
            return hashMap;
        }
        hashMap.put("status","1");
        hashMap.put("msg","插入成功");
        return hashMap;
    }



}
